package boogui.admin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import boogui.db.BooguiDAO;
import boogui.db.IBooguiDAO;
import boogui.movie.MovieAreaVO;

public class MovieRegistryDAO {
	IBooguiDAO bdao = null;
	MovieRegistrySQL sql = null;

	public MovieRegistryDAO() {
		bdao = new BooguiDAO();
		sql = new MovieRegistrySQL();
	}

	// 모든 지역
	public List<MovieAreaVO> allLocation() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MovieAreaVO> result = new ArrayList<>();

		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(sql.getAllLocationSQL());
			rs = pstmt.executeQuery();

			while (rs.next()) {
				MovieAreaVO mv = new MovieAreaVO();
				mv.setLocation(rs.getString("location"));
				result.add(mv);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}
		return result;
	}
	
	//해당 지역 영화관 이름
	public List<MovieAreaVO> locationCinema(String list) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MovieAreaVO> result = new ArrayList<>();
		
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(sql.getCinemaSQL());
			pstmt.setString(1, list);
			rs = pstmt.executeQuery();	

			while (rs.next()) {
				MovieAreaVO mv = new MovieAreaVO();
				mv.setCinename(rs.getString("cinename"));
				result.add(mv);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}
		return result;
	}
	// 지역, 영화관, 상영관 조회
	public List<MovieAreaVO> locationTheater(String sql, String list, String fieldname){
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MovieAreaVO> result = new ArrayList<>();
		
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, list);
			rs = pstmt.executeQuery();			
			
			while (rs.next()) {
				MovieAreaVO mv = new MovieAreaVO();
				mv.setCinename(rs.getString(fieldname));
				result.add(mv);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}
		return result;
	}	
	//해당 영화관의 상영관
	public List<MovieAreaVO> cinemaTheater(String list) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MovieAreaVO> result = new ArrayList<>();
		
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(sql.getTheaterSQL());
			pstmt.setString(1, list);
			rs = pstmt.executeQuery();	
			
			while (rs.next()) {
				MovieAreaVO mv = new MovieAreaVO();
				mv.setCinename(rs.getString("theater"));
				result.add(mv);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}
		return result;
	}
}
